package com.example.heimaai.controller;

import com.example.heimaai.entity.VO.MessageVO;
import com.example.heimaai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/ai/history")
public class ChatHistoryController {
    private final ChatHistoryRepository chatHistoryRepository;
    private final ChatMemory chatMemory;
    @GetMapping("/{type}")
    public List<String> getHistoryChatList(@PathVariable("type") String type){
        List<String> chatIds = chatHistoryRepository.getChatIds(type);
        if (chatIds==null||chatIds.isEmpty()){
            return List.of();
        }
        return chatIds;
    }
    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getHistoryChatById(@PathVariable("type") String type, @PathVariable("chatId") String chatId){
        List<Message> messages = chatMemory.get(chatId);
        if (messages==null||messages.isEmpty()){
            return List.of();
        }
        List<MessageVO> list = messages.stream().map(MessageVO::new).toList();
        return list;
    }
}
